<template>
  <el-dialog
    class="addedit-product-dialog"
    width="900px"
    :visible.sync="dialogVisible"
    :title="operType == 'add' ? '新增产品/服务' : '修改产品/服务'"
    :close-on-click-modal="false"
    :append-to-body="true"
    v-dialogDrag="true"
    @open="handleDialogOpen"
    @closed="handleDialogClosed"
  >
    <el-form ref="addEditProductForm" :model="formData" :rules="formRules" size="small" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="编号:" prop="productCode">
            <el-input v-model="formData.productCode" placeholder="请输入" :clearable="false">
              <div
                slot="suffix"
                style="color: #409EFF; cursor: pointer; padding: 0 5px;"
                @click="handleGenerateNumber"
              >自动生成</div>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="名称:" prop="productName">
            <el-input v-model="formData.productName" placeholder="请输入" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格型号:" prop="specifications">
            <el-input v-model="formData.specifications" placeholder="请输入" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="单位:" prop="unit">
            <el-select
              style="width: 100%;"
              v-model="formData.unit"
              clearable
              filterable
            >
              <el-option
                v-for="(item, index) in dict.type.product_unit"
                :key="index"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="dialog-footer" slot="footer">
      <el-button style="margin-right: 50px;" type="primary" @click="handleSave">保存</el-button>
      <el-button @click="dialogVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  productDetail,
  productAdd,
  productEdit,
  genProductNumber
} from '@/api/person/product'

export default {
  name: 'AddEditProductDialog',
  components: {},
  dicts: ['product_unit'],
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    operType: {
      type: String,
      default: 'add',
    },
    row: {
      type: Object,
      default: () => ({})
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show
      },
      set(value) {
        this.$emit('update:show', value)
      }
    }
  },
  data() {
    return {
      formData: {},
      formRules: {
        productCode: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        productName: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        specifications: [],
        unit: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
      },
    }
  },
  created() {
    this.initFormData()
  },
  methods: {
    // 弹框打开时
    handleDialogOpen() {
      if (this.operType == 'edit') {
        productDetail(this.row.productId).then(res => {
          for (let key in this.formData) {
            this.formData[key] = res.data[key]
          }
        }).catch(err => {})
      }
    },
    // 弹框关闭时
    handleDialogClosed() {
      this.initFormData()
      this.$refs.addEditProductForm.resetFields()
    },
    // 初始化表单数据
    initFormData() {
      this.formData = {
        productCode: '',
        productName: '',
        specifications: '',
        unit: '',
      }
    },
    // 获取表单数据
    getFormData() {
      const data = JSON.parse(JSON.stringify(this.formData))
      if (this.operType == 'edit') {
        data.productId = this.row.productId
      }
      return data
    },
    // 自动生成编号
    handleGenerateNumber() {
      genProductNumber().then(res => {
        this.formData.productCode = res.data || ''
      }).catch(err => {})
    },
    // 点击保存
    handleSave() {
      this.$refs.addEditProductForm.validate(valid => {
        if (!valid) return
        let fn = null
        if (this.operType == 'add') fn = productAdd
        else if (this.operType == 'edit') fn = productEdit
        const data = this.getFormData()
        fn(data).then(res => {
          this.$message.success('操作成功')
          this.dialogVisible = false
          this.$emit('refresh')
        }).catch(err => {})
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.addedit-product-dialog {

  ::v-deep .el-dialog__body {
    max-height: 70vh;
    overflow-y: auto;
  }
}
</style>
